﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyBepAnTuThien
{
    public partial class frmSuaBienBanBanGiao : Form
    {
        public frmSuaBienBanBanGiao()
        {
            InitializeComponent();
        }
        connect cn = new connect();
        private void loaddata()
        {
            //cobDungCu.DataSource = cn.getDataTable("tblDungCu_NguyenLieu_DS");
            //cobDungCu.DisplayMember = "TenDungCu";
            //cobDungCu.ValueMember = "MaDungCu";

            cobToGiao.DataSource = cn.getDataTable("tblTo_DS");
            cobToGiao.DisplayMember = "TenTo";
            cobToGiao.ValueMember = "MaTo";

            cobToNhan.DataSource = cn.getDataTable("tblTo_DS");
            cobToNhan.DisplayMember = "TenTo";
            cobToNhan.ValueMember = "MaTo";

            cobNguoiGiao.DataSource = cn.getDataTable("tblNhanVien_DS");
            cobNguoiGiao.DisplayMember = "HoTen";
            cobNguoiGiao.ValueMember = "MaNV";

            cobNguoiNhan.DataSource = cn.getDataTable("tblNhanVien_DS");
            cobNguoiNhan.DisplayMember = "HoTen";
            cobNguoiNhan.ValueMember = "MaNV";

            SqlCommand cmd = new SqlCommand("tblChiTietBanGiao_DS_Sua", cn.ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("mabienban",SqlDbType.Int)).Value = bientinhBienBanBanGiao.Mabienban;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            gridBienBan.DataSource = dt;
        }
        private void frmSuaBienBanBanGiao_Load(object sender, EventArgs e)
        {
            try
            {
                loaddata();
                txtMaBienBan.Text = bientinhBienBanBanGiao.Mabienban.ToString();
                dtNgayGiao.Value = bientinhBienBanBanGiao.Ngaylap;
                cobToGiao.SelectedValue = bientinhBienBanBanGiao.Togiao;
                cobToNhan.SelectedValue = bientinhBienBanBanGiao.Tonhan;
                cobNguoiGiao.SelectedValue = bientinhBienBanBanGiao.Nguoigiao;
                cobNguoiNhan.SelectedValue = bientinhBienBanBanGiao.Nguoinhan;
                (gridBienBan.Columns["cbHienVat"] as DataGridViewComboBoxColumn).DataSource = cn.getDataTable("tblDungCu_NguyenLieu_DS");
                (gridBienBan.Columns["cbHienVat"] as DataGridViewComboBoxColumn).DisplayMember = "TenDungCu";
                (gridBienBan.Columns["cbHienVat"] as DataGridViewComboBoxColumn).ValueMember = "MaDungCu";
            }
            catch
            {
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            int i;
            for (i = 0; i < gridBienBan.Rows.Count; i++)
            {
                DataGridViewComboBoxCell c = (DataGridViewComboBoxCell)gridBienBan.Rows[i].Cells["cbHienVat"];
                int madungcu = int.Parse(c.Value.ToString());
                double soluong = double.Parse(gridBienBan.Rows[i].Cells["sl"].Value.ToString());
                int id = Convert.ToInt32(gridBienBan.Rows[i].Cells["stt"].Value.ToString());
                string tinhtrang = gridBienBan.Rows[i].Cells["tt"].Value.ToString();
                SqlCommand cmd_insert = new SqlCommand("tblChiTietBanGiao_Sua", cn.ketnoi());
                cmd_insert.CommandType = CommandType.StoredProcedure;
                cmd_insert.Parameters.Add(new SqlParameter("madungcu", SqlDbType.Int)).Value = madungcu;
                cmd_insert.Parameters.Add(new SqlParameter("soluong", SqlDbType.Int)).Value = soluong;
                cmd_insert.Parameters.Add(new SqlParameter("tinhtrang", SqlDbType.NVarChar)).Value = tinhtrang;
                cmd_insert.Parameters.Add(new SqlParameter("stt", SqlDbType.Int)).Value = id;
                cmd_insert.ExecuteNonQuery();
            }
            MessageBox.Show("Thành Công","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            this.Close();
        }
    }
}
